using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CodePlex.Resourceful
{
    public class SimpleRetryStrategy : ExponentialBackoffRetryStrategy
    {
        public SimpleRetryStrategy(int maxRetries): base(maxRetries,TimeSpan.Zero,1)
        {

        }
    }


    public class ExponentialBackoffRetryStrategy : IRetryStrategy
    {
        private readonly int _retriesLeft;
        private readonly TimeSpan _timeBetweenRetries;
        private readonly double _backoffMultiplier;

        private readonly bool _isFirst;


        public ExponentialBackoffRetryStrategy(int maxRetries, TimeSpan timeBetweenRetries, double backoffMultiplier)
            : this(maxRetries,timeBetweenRetries,backoffMultiplier,true)
        {

        }
        private ExponentialBackoffRetryStrategy(int maxRetries, TimeSpan timeBetweenRetries,double backoffMultiplier, bool isFirst)
        {
            _retriesLeft = maxRetries;
            _timeBetweenRetries = timeBetweenRetries;
            _backoffMultiplier = backoffMultiplier;
            _isFirst = isFirst;
        }

        public bool IsAllowed
        {
            get
            {
                return _retriesLeft > 0;
            }
        }

        public TimeSpan WaitSpan
        {
            get 
            { 
                return _isFirst?TimeSpan.Zero: _timeBetweenRetries; 
            }
        }


        public IRetryStrategy Next()
        {
            return new ExponentialBackoffRetryStrategy(
                _retriesLeft - 1,
                _isFirst?_timeBetweenRetries:TimeSpan.FromMilliseconds(_timeBetweenRetries.TotalMilliseconds * _backoffMultiplier),
                _backoffMultiplier,
                false);
        }

       



       
    }




}
